home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / misc / ini_library / sources / c / ini_example / ini_example.c < prev    next >
C/C++ Source or Header  |  1999-11-30  |  13KB  |  540 lines

  1. /************************************************
  2. ***  Complex example program for INI library  ***
  3. ***          © 1999 by Basty/Seasons          ***
  4. ***   This program demonstrates the powerful  ***
  5. ***       functions of the INI library        ***
  6. ***               C/C++ version               ***
  7. ***  StormC 3.0 DSK was used for development  ***
  8. ************************************************/
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #include <exec/types.h>
  15. #include <exec/libraries.h>
  16. #include <exec/lists.h>
  17. #include <exec/nodes.h>
  18. #include <libraries/dosextens.h>
  19. #include <libraries/ini_lib.h>
  20. #include <proto/exec.h>
  21. #include <proto/intuition.h>
  22. #include <proto/graphics.h>
  23. #include <proto/ini.h>
  24. #include <intuition/intuitionbase.h>
  25. #include <intuition/screens.h>
  26. #include <intuition/intuition.h>
  27. #include <graphics/rastport.h>
  28.  
  29. #include "INI_Example.h"
  30.  
  31. #define DefaultScrTitle "Example INI Test Screen © 1999 by Basty/Seasons"
  32. #define DefaultWinTitle "Example INI Test Window © 1999 by Basty/Seasons"
  33.  
  34. struct INIScreen
  35. {
  36.   struct MinList iniscr_ScrList;
  37. };
  38.  
  39. struct ScreenList
  40. {
  41.   struct MinNode iniscr_MinNode;
  42.   struct Screen *iniscr_ScreenPtr;
  43.   struct MinList iniscr_WinList;
  44. };
  45.  
  46. struct ScreenWinList
  47. {
  48.   struct MinNode scrwin_MinNode;
  49.   struct Window *scrwin_WindowPtr;
  50. };
  51.  
  52. struct iniLibBase *INIBase;
  53. struct IntuitionBase *IntuitionBase;
  54. struct GfxBase *GfxBase;
  55. struct iniFile *DefINIFile;
  56. struct ScreenList WBScreen =
  57. {
  58.   {NULL, NULL},
  59.   NULL,
  60.   {(struct MinNode *) &(WBScreen.iniscr_WinList.mlh_Tail),NULL,(struct MinNode *) &(WBScreen.iniscr_WinList.mlh_Head)}
  61. };
  62.  
  63. struct INIScreen Screens =
  64. {
  65.   {(struct MinNode *) &(WBScreen.iniscr_MinNode.mln_Succ),NULL,(struct MinNode *) &(WBScreen.iniscr_MinNode.mln_Succ)}
  66. };
  67.  
  68. UWORD DefaultColTab[256] =
  69. {
  70.   0x000, 0xFFF, 0x777, 0xCCC,
  71.   0x444, 0x555, 0x666, 0x888,
  72.   0x111, 0x222, 0xAAA, 0x333,
  73.   0x999, 0xDDD, 0xEEE, 0xBBB
  74. };
  75.  
  76. struct NewScreen NewScreenStr =
  77. {
  78.   0, 0, /* LeftEdge, TopEdge */
  79.   640, 256, /* Width, Height */
  80.   4, /* Depth */
  81.   1, 0, /* DetailPen, BlockPen */
  82.   0x8000, /* ViewModes */
  83.   CUSTOMSCREEN, /* Type */
  84.   0, 0, /* TextAttr, DefaultTitle, */
  85.   0, 0 /* Gadgets, BitMap */
  86. };
  87.  
  88. struct NewWindow NewWindowStr =
  89. {
  90.   0, 0, /* LeftEdge, TopEdge */
  91.   640, 256, /* Width, Height */
  92.   1, 0, /* DetailPen, BlockPen */
  93.   0x200, 0x100F, /* IDCMPFlags, Flags */
  94.   0, 0, /* Gadget, Image */
  95.   0, 0, 0, /* Title, Screen, BitMap */
  96.   0, 0, /* MinWidth, MinHeight */
  97.   640, 256, /* MaxWidth, MaxHeight */
  98.   CUSTOMSCREEN /* Type */
  99. };
  100.  
  101. unsigned char ScreenContext[16] = "Screen";
  102. unsigned char WindowContext[16] = "Window";
  103.  
  104. void InitNode (struct List *NodeList)
  105. {
  106.   NodeList->lh_Head = (struct Node *) &(NodeList->lh_Tail);
  107.   NodeList->lh_Tail = (struct Node *) NULL;
  108.   NodeList->lh_TailPred = (struct Node *) &(NodeList->lh_Head);
  109. }
  110.  
  111. int ReadINIScreens (struct iniFile *iniFile)
  112. {
  113.   struct iniContext *CurrentContext;
  114.   struct ScreenList *ScrStrBuf;
  115.   struct Screen *ScreenStr;
  116.   unsigned long ScrNumCols;
  117.   UWORD ScrColorTable[256];
  118.  
  119.   /* Set FAIL error mark */
  120.  
  121.   int rc = 20;
  122.  
  123.   /* Start with first screen */
  124.  
  125.   unsigned long CurrentScrNum = 1;
  126.  
  127.   for (;;)
  128.   {
  129.  
  130.     /* Convert screen number to ASCII using standard decimal format */
  131.  
  132.     iniIntToStr ( ScreenContext+6, CurrentScrNum, INI_FORMAT_DEC, 0L, ' ');
  133.  
  134.     /* Increment screen counter */
  135.  
  136.     CurrentScrNum++;
  137.  
  138.     /* Search for context and check if found */
  139.  
  140.     if (!(CurrentContext = iniFindContext ( iniFile, ScreenContext, 0L)))
  141.     {
  142.       /* Found? No -> end of search */
  143.       rc = 0;
  144.       break;
  145.     }
  146.  
  147.     /* Get left edge value or use default value of zero. */
  148.  
  149.     NewScreenStr.LeftEdge = iniReadLong ( iniFile, ScreenContext,
  150.       "LeftEdge", 0L, 0L);
  151.  
  152.     /* Get top edge value or use default value of zero. */
  153.  
  154.     NewScreenStr.TopEdge = iniReadLong ( iniFile, ScreenContext,
  155.       "TopEdge", 0L, 0L);
  156.  
  157.     /* Get width value or default width of 640 pixels. */
  158.  
  159.     NewScreenStr.Width = iniReadLong ( iniFile, ScreenContext,
  160.       "Width", 640L, 0L);
  161.  
  162.     /* Get height value or default height of 256 pixels. */
  163.  
  164.     NewScreenStr.Height = iniReadLong ( iniFile, ScreenContext,
  165.       "Height", 256L, 0L);
  166.  
  167.     /* Get depth value or default of 4 bitplanes (16 colors). */
  168.  
  169.     NewScreenStr.Depth = iniReadLong ( iniFile, ScreenContext,
  170.       "Depth", 4L, 0L);
  171.  
  172.     /* Get view modes value or default view mode of HIRES (0x8000). */
  173.  
  174.     NewScreenStr.ViewModes = iniReadLong ( iniFile, ScreenContext,
  175.       "ViewModes", 0x8000L, 0L);
  176.  
  177.     /* Get screen default title or use default string */
  178.  
  179.     NewScreenStr.DefaultTitle = iniReadStr ( iniFile, ScreenContext,
  180.       "Title", DefaultScrTitle, 0L);
  181.  
  182.     /* Copy all 256 default colors to updated color table */
  183.  
  184.     memcpy (ScrColorTable,DefaultColTab,256);
  185.     ScrNumCols = (NewScreenStr.Depth < 8 ? 1L<<NewScreenStr.Depth : 256L);
  186.  
  187.     /* Read all necessary color table entries into color table */
  188.  
  189.     iniReadWordA ( iniFile, ScreenContext, "ColorTable", (WORD *) &ScrColorTable,
  190.       ScrNumCols, 0L);
  191.  
  192.     /* We need storage buffers for our screens */
  193.  
  194.     if (!(ScrStrBuf = (struct ScreenList *) iniAllocPMem (sizeof(struct ScreenList))))
  195.     {
  196.       /* Deallocate name string buffer. Don't forget ! */
  197.  
  198.       iniFreeNameStr (NewScreenStr.DefaultTitle);
  199.       break;
  200.     }
  201.  
  202.     /* Add them to list */
  203.  
  204.     AddTail ((struct List *) &Screens.iniscr_ScrList, (struct Node *) ScrStrBuf);
  205.  
  206.     /* Open the screen */
  207.  
  208.     ScreenStr = OpenScreen (&NewScreenStr);
  209.  
  210.     ScrStrBuf->iniscr_ScreenPtr = ScreenStr;
  211.  
  212.     /* Initialize window node list */
  213.  
  214.     InitNode ((struct List *) &(ScrStrBuf->iniscr_WinList));
  215.  
  216.     if (ScreenStr == NULL)
  217.     {
  218.       /* Deallocate name string buffer. Don't forget */
  219.  
  220.       iniFreeNameStr (NewScreenStr.DefaultTitle);
  221.       break;
  222.     }
  223.  
  224.     LoadRGB4 (&ScreenStr->ViewPort, ScrColorTable, ScrNumCols);
  225.   }
  226.  
  227.   return ( rc );
  228. }
  229.  
  230. int ReadINIWindows (struct iniFile *iniFile)
  231. {
  232.   struct iniContext *CurrentContext;
  233.   struct ScreenWinList *WinStrBuf;
  234.   struct Window *WindowStr;
  235.   struct ScreenList *scr;
  236.   unsigned long ScreenNum,i;
  237.  
  238.   /* Set FAIL error mark */
  239.  
  240.   int rc = 20;
  241.  
  242.   /* Start with first window */
  243.  
  244.   unsigned long CurrentWinNum = 1;
  245.  
  246.   for (;;)
  247.   {
  248.  
  249.     /* Convert window number to ASCII using standard decimal format */
  250.  
  251.     iniIntToStr ( WindowContext+6, CurrentWinNum, INI_FORMAT_DEC, 0L, ' ');
  252.  
  253.     /* Increment window counter */
  254.  
  255.     CurrentWinNum++;
  256.  
  257.     /* Search for context and check if found */
  258.  
  259.     if (!(CurrentContext = iniFindContext ( iniFile, WindowContext, 0L)))
  260.     {
  261.       /* Found? No -> end of search */
  262.       rc = 0;
  263.       break;
  264.     }
  265.  
  266.     /* Get left edge value or use default value of zero. */
  267.  
  268.     NewWindowStr.LeftEdge = iniReadLong ( iniFile, WindowContext,
  269.       "LeftEdge", 0L, 0L);
  270.  
  271.     /* Get top edge value or use default value of zero. */
  272.  
  273.     NewWindowStr.TopEdge = iniReadLong ( iniFile, WindowContext,
  274.       "TopEdge", 0L, 0L);
  275.  
  276.     /* Get width value or default width of 640 pixels. */
  277.  
  278.     NewWindowStr.Width = iniReadLong ( iniFile, WindowContext,
  279.       "Width", 640L, 0L);
  280.  
  281.     /* Get height value or default height of 256 pixels. */
  282.  
  283.     NewWindowStr.Height = iniReadLong ( iniFile, WindowContext,
  284.       "Height", 256L, 0L);
  285.  
  286.     /* Get IDCMP flags or default to 0x20000 (borderless). */
  287.  
  288.     NewWindowStr.IDCMPFlags = iniReadLong ( iniFile, WindowContext,
  289.       "IDCMP", 0x20000, 0L);
  290.  
  291.     /* Get window flags or default to 0x100F (Autoactivate + all gadgets) */
  292.  
  293.     NewWindowStr.Flags = iniReadLong ( iniFile, WindowContext,
  294.       "Flags", 0x100FL, 0L);
  295.  
  296.     /* Get screen default title or use default string */
  297.  
  298.     NewWindowStr.Title = iniReadStr ( iniFile, WindowContext,
  299.       "Title", DefaultWinTitle, 0L);
  300.  
  301.     /* Get min width value or default min width of 32 pixels. */
  302.  
  303.     NewWindowStr.MinWidth = iniReadLong ( iniFile, WindowContext,
  304.       "MinWidth", 32L, 0L);
  305.  
  306.     /* Get max width value or default max width of 640 pixels. */
  307.  
  308.     NewWindowStr.MaxWidth = iniReadLong ( iniFile, WindowContext,
  309.       "MaxWidth", 640L, 0L);
  310.  
  311.     /* Get min height value or default min height of 32 pixels. */
  312.  
  313.     NewWindowStr.MinHeight = iniReadLong ( iniFile, WindowContext,
  314.       "MinHeight", 32L, 0L);
  315.  
  316.     /* Get max height value or default max height of 256 pixels. */
  317.  
  318.     NewWindowStr.MaxHeight = iniReadLong ( iniFile, WindowContext,
  319.       "MaxHeight", 256L, 0L);
  320.  
  321.     /* Get screen number or use default screen (first screen). */
  322.  
  323.     ScreenNum = iniReadLong ( iniFile, WindowContext,
  324.       "Screen", 1L, 0L);
  325.  
  326.     /* Find wanted screen */
  327.  
  328.     scr = (struct ScreenList *) &(Screens.iniscr_ScrList.mlh_Head);
  329.  
  330.     for (i = ScreenNum+1 ; i > 0 ; i--)
  331.     {
  332.  
  333.       /* Next screen */
  334.       scr = (struct ScreenList *) scr->iniscr_MinNode.mln_Succ;
  335.  
  336.       if (scr->iniscr_MinNode.mln_Succ == NULL)
  337.       {
  338.         /* Yes -> done */
  339.         rc = 0;
  340.         break;
  341.       }
  342.     }
  343.  
  344.     /* Check if last entry was found */
  345.  
  346.     if (rc == 0)
  347.     {
  348.       rc = 20;
  349.       continue;
  350.     }
  351.  
  352.     /* Copy screen pointer */
  353.  
  354.     NewWindowStr.Screen = scr->iniscr_ScreenPtr;
  355.  
  356.     /* We need storage buffers for our windows */
  357.  
  358.     if (!(WinStrBuf = (struct ScreenWinList *) iniAllocPMem (sizeof(struct ScreenWinList))))
  359.     {
  360.       /* Deallocate name string buffer. Don't forget ! */
  361.  
  362.       iniFreeNameStr (NewWindowStr.Title);
  363.       break;
  364.     }
  365.  
  366.     /* Add them to list */
  367.  
  368.     AddTail ((struct List *) &scr->iniscr_WinList, (struct Node *) WinStrBuf);
  369.  
  370.     /* Open the window */
  371.  
  372.     WindowStr = OpenWindow (&NewWindowStr);
  373.  
  374.     if ((WinStrBuf->scrwin_WindowPtr = WindowStr) == NULL)
  375.     {
  376.       /* Deallocate name string buffer. Don't forget */
  377.  
  378.       iniFreeNameStr (NewWindowStr.Title);
  379.       break;
  380.     }
  381.   }
  382.  
  383.   return ( rc );
  384. }
  385.  
  386. void CloseIntStuff(struct INIScreen *scrlist)
  387. {
  388.   struct ScreenList *scr;
  389.   struct ScreenWinList *win,*oldwin;
  390.     char *Title;
  391.  
  392.   for (;;)
  393.   {
  394.  
  395.     /* Next screen */
  396.  
  397.     scr = (struct ScreenList *) scrlist->iniscr_ScrList.mlh_Head;
  398.  
  399.     /* Have we reached last entry? */
  400.  
  401.     if (scr->iniscr_MinNode.mln_Succ == NULL)
  402.       /* Yes -> done */
  403.       break;
  404.  
  405.     /* Get window pointers */
  406.  
  407.     oldwin = (struct ScreenWinList *) &(scr->iniscr_WinList.mlh_Head);
  408.     win = (struct ScreenWinList *) oldwin->scrwin_MinNode.mln_Succ; 
  409.     for (;;)
  410.     {
  411.       /* Get next window pointer */
  412.  
  413.       oldwin = win;
  414.       if (win->scrwin_MinNode.mln_Succ == NULL)
  415.         /* Yes -> done */
  416.         break;
  417.  
  418.       /* Get window to close and check if valid pointer */
  419.  
  420.       if ( win->scrwin_WindowPtr != NULL)
  421.       {
  422.         /* Save window title for dealloc */
  423.  
  424.         Title = win->scrwin_WindowPtr->Title;
  425.  
  426.         /* Pointer valid? -> Close the damn window! */
  427.  
  428.         CloseWindow ( win->scrwin_WindowPtr );
  429.  
  430.         /* Deallocate name string buffer. Don't forget */
  431.  
  432.         iniFreeNameStr (Title);
  433.  
  434.       }
  435.  
  436.       win = (struct ScreenWinList *) win->scrwin_MinNode.mln_Succ;
  437.  
  438.       /* Deallocate memory using ini.library */
  439.  
  440.       iniFreePMem ( oldwin, sizeof(struct ScreenWinList));
  441.  
  442.    };
  443.  
  444.     /* Remove node entry before dealloc */
  445.  
  446.     Remove ( (struct Node *) scr );
  447.  
  448.     if (scr->iniscr_ScreenPtr == WBScreen.iniscr_ScreenPtr)
  449.       continue;
  450.  
  451.     /* Save screen title for dealloc */
  452.  
  453.     Title = scr->iniscr_ScreenPtr->Title;
  454.  
  455.     /* Close screen */
  456.  
  457.     CloseScreen (scr->iniscr_ScreenPtr );
  458.  
  459.     /* Deallocate name string buffer. Don't forget */
  460.  
  461.     iniFreeNameStr (Title);
  462.  
  463.     /* Deallocate memory */
  464.  
  465.     iniFreePMem ( scr, sizeof(struct ScreenList));
  466.   };
  467. }
  468.  
  469. void main()
  470. {
  471.   /* Set FAIL error mark */
  472.  
  473.   int rc = 20;
  474.  
  475.   /* Load v31.00 of ini.library */
  476.  
  477.   if (!( INIBase = (struct iniLibBase *) OpenLibrary("ini.library",31L)))
  478.     exit ( rc );
  479.  
  480.   /* Load intuiton.library (any version) */
  481.  
  482.   IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library",0L);
  483.  
  484.   /* Load graphics.library (any version) */
  485.  
  486.   GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",0L);
  487.  
  488.   /* Initialize first screen node */
  489.  
  490.   WBScreen.iniscr_MinNode.mln_Succ = (struct MinNode *) &(Screens.iniscr_ScrList.mlh_Tail);
  491.   WBScreen.iniscr_MinNode.mln_Pred = (struct MinNode *) &(Screens.iniscr_ScrList.mlh_Head);
  492.  
  493.   /* Get first screen (WB screen) */
  494.  
  495.   WBScreen.iniscr_ScreenPtr = IntuitionBase->FirstScreen;
  496.  
  497.   /* Open :Example.INI INI file/create it */
  498.  
  499.   if (!(DefINIFile = iniOpenDefault( DefaultINI, ":Example.INI", sizeof(DefaultINI))))
  500.     goto CloseINI;
  501.  
  502.   rc = ReadINIScreens(DefINIFile);
  503.  
  504.   if (rc != 0)
  505.     goto CloseINI;
  506.  
  507.   rc = ReadINIWindows(DefINIFile);
  508.  
  509.   if (rc != 0)
  510.     goto CloseINI;
  511.  
  512.   /* Wait for mouse button */
  513.  
  514.   while (*( unsigned char *)0xBFE001 & 0x40);
  515.  
  516. CloseINI:
  517.  
  518. /* We want to deallocate all screens */
  519.  
  520.   CloseIntStuff(&Screens);
  521.   
  522.   iniClose ( DefINIFile );
  523.  
  524.   /* Close graphics.library */
  525.  
  526.   CloseLibrary ( (struct Library *) GfxBase );
  527.  
  528.   /* Close intuition.library */
  529.  
  530.   CloseLibrary ( (struct Library *) IntuitionBase );
  531.  
  532.   /* Close INI library */
  533.  
  534.   CloseLibrary ( (struct Library *) INIBase );
  535.  
  536.   /* Exit to DOS */
  537.  
  538.   exit ( rc );
  539. }
  540.